//获取当前账户下的商品
$(document).ready(function() {
    // 接收页面传递的参数id
    let id = location.search.split("=")[1];


    $.ajax({
        url: "http://jx.xuzhixiang.top/ap/api/detail.php",
        type: "get",
        data: { id: id },
        success: function(res) {
            console.log(res);

            let detail = document.querySelector(".detail");
            let element = `
				<img src="${res.data.pimg}">
				<p>${res.data.pname}</p>
				<p>${res.data.pprice}元</p>
				<span class="minus">-</span><input type="text" value=1 class="num"><span class="plus">+</span>
        
        <input type="button" data-id="${res.data.pid}" value="加入购物车" class="addBtn" />
       
			`;

            detail.innerHTML = element;

            let oMinus = document.querySelector(".minus");
            let oPlus = document.querySelector(".plus");
            let oNum = document.querySelector(".num");

            let pid = $(".addBtn").attr("data-id");
            console.log(pid);


            oMinus.onclick = function() {
                oNum.value--;
                if (oNum.value < 1) {
                    oNum.value = 1;
                }
            };
            oPlus.onclick = function() {
                oNum.value++;
            };
            oNum.onchange = function() {
                if (oNum.value < 1) {
                    oNum.value = 1;
                }
            };
            $(".addBtn").click(function() {
                $.ajax({
                    url: "http://jx.xuzhixiang.top/ap/api/add-product.php",
                    type: "get",
                    data: {
                        uid: 69007,
                        pid: pid,
                        pnum: oNum.value,
                    },
                    success: function(res) {
                        console.log(res);
                        location.href = "../html/cart.html";
                    },
                });
            });
        },
    });
});